﻿using System;
using System.Data;
using EOSCRM.Web.Common;
using CrystalDecisions.CrystalReports.Engine;
using EOSCRM.Util;
using EOSCRM.Dao;
using EOSCRM.Web.Shared;
using ReportDocument = CrystalDecisions.CrystalReports.Engine.ReportDocument;

namespace EOSCRM.Web.Forms.DongHo.BaoCao
{
    public partial class DanhSachDongHoKhachHang : Authentication
   {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                
                
                if (!Page.IsPostBack)
                {
                    LoadReferences();
                }
                else 
                {
                    var dt = (DataTable)Session[SessionKey.DH_BAOCAO_THONGKEDONGHOKHACHHANG];
                    Report(dt);
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void LoadReferences()
        {
            CommonFunc.BindDropDownList(ddlKHUVUC, new KhuVucDao().GetList(), "TENKV", "MAKV", true);
            txtNguoiLap.Text = LoginInfo.NHANVIEN.HOTEN;

            var currentDate = DateTime.Now;
            var firstDayInMonth = new DateTime(currentDate.Year, currentDate.Month, 1);
            txtTuNgay.Text = firstDayInMonth.ToString(ConfigDao.Value(DATEFORMAT.F_DDMMYYYY.ToString()));
            txtDenNgay.Text = currentDate.ToString(ConfigDao.Value(DATEFORMAT.F_DDMMYYYY.ToString()));
        }

        #region Startup script registeration

        private void CloseWaitingDialog()
        {
            var master = (EOS)Page.Master;
            if (master != null) master.CloseWaitingDialog();
        }

        #endregion

        protected void btnBaoCao_Click(object sender, EventArgs e)
        {
            var TuNgay = DateTimeUtil.GetVietNamDate(txtTuNgay.Text.Trim());
            var DenNgay = DateTimeUtil.GetVietNamDate(txtDenNgay.Text.Trim());

            var ds =
                new ReportClass().DanhSachDongHoKhachHang(TuNgay, DenNgay, ddlKHUVUC.SelectedValue);
            if (ds == null || ds.Tables.Count == 0) { CloseWaitingDialog(); return; }

            Report(ds.Tables[0]);
            CloseWaitingDialog();
        }

        private void Report(DataTable dt)
        {
            if (dt == null)
                return;

            var rp = (ReportDocument)Session[Constants.REPORT_FREE_MEM];
            if (rp != null)
            {
                try
                {
                    rp.Close();
                    rp.Dispose();
                    GC.Collect();
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {

                }
            }

            rp = new ReportDocument();

            SetTitle(rp);

            rp.SetDataSource(dt);
            rpViewer.ReportSource = rp;
            rpViewer.DataBind();

            divreport.Visible = true;

            Session[SessionKey.DH_BAOCAO_THONGKEDONGHOKHACHHANG] = dt;
            Session[Constants.REPORT_FREE_MEM] = rp;
        }

        private void SetTitle(ReportDocument rp)
        {
            var path = Server.MapPath("../../../Reports/KiemDinhDongHo/DanhSachDongHoKhachHang.rpt");
            rp.Load(path);
            string[] controls = { "txtNgayThangNam", "txtNguoiLap","txtTitle" };
            string[] values = { ReportFuntions.GetNgayThangNam(), txtNguoiLap.Text.Trim(),"DANH SÁCH ĐỒNG HỒ KHÁCH HÀNG" };
            ReportFuntions._SetTitle(ref rp, controls, values);

            var txtTuNgay1 = rp.ReportDefinition.ReportObjects["txtTUNGAY"] as TextObject;
            if (txtTuNgay1 != null) txtTuNgay1.Text = txtTuNgay.Text.Trim();

            var txtDenNgay1 = rp.ReportDefinition.ReportObjects["txtDENNGAY"] as TextObject;
            if (txtDenNgay1 != null) txtDenNgay1.Text = txtDenNgay.Text.Trim();
        }
    }
}